var express = require('express');
var router = express.Router();
var utilFs = require('../utils/fs')


router.get('/', function(req, res, next) {
	var host = req.headers.host;
  utilFs.readdir('public/music')
	  .then((val) => {
	  	var result = getUrl(val,host);
	  	if(!result){
	  		res.json({
	  			status:1002,
	  			msg:'音乐列表为空'
	  		})
	  	}else{
	  		res.json({
	  			status:1001,
	  			msg:'success',
	  			list:result
	  		})
	  	}
	  },(err) => {
	  	console.log(err)
	  })
});

function getUrl(val,host){
	if(val.length==0) return false;
	var urlArr = [];
	for(var i=0;i<val.length;i++){
		var obj = {
				url :'http://' + host +'/music/'+ val[i],
				name : val[i]
		}
		urlArr.push(obj)
	}
	return urlArr;
}

module.exports = router;
